/*
* entity.cpp
* Entity class methods definition
*/
#include <string>

#include "entity.h"

using std::string;

/* Constructor
*
*/
Entity::Entity() {
  _life = 60;
}

/* Ent Lib Loader
*
*/
void Entity::entLibInit() {
  int i,j;
  for(i = 0; i < ENTITY_LIBRARY_SIZE; ++i)
    for(j = 0; j < ENTITY_ANIMATION_SIZE; ++j)
      entArr[i][j] = 0x00;
  // This part is hardcoded on purpose
  entArr[0][0] = load_bitmap("images/bullets/gun_1.bmp", NULL);
  entArr[0][1] = load_bitmap("images/bullets/gun_2.bmp", NULL);
  entArr[0][2] = load_bitmap("images/bullets/gun_3.bmp", NULL);
  entArr[1][0] = load_bitmap("images/bullets/enemy_gun_1.bmp", NULL);
  entArr[1][1] = load_bitmap("images/bullets/enemy_gun_2.bmp", NULL);
  entArr[1][2] = load_bitmap("images/bullets/enemy_gun_3.bmp", NULL);
  entArr[2][0] = load_bitmap("images/bullets/hit.bmp", NULL);
}

int Entity::__getType() { return _type; }
/* Ent Lib Destructor
*
*/
void Entity::entLibDestroy() {
  int i,j;
  for(i = 0; i < ENTITY_LIBRARY_SIZE; ++i)
    for(j = 0; j < ENTITY_ANIMATION_SIZE; ++j)
      if ( entArr[i][j] ) destroy_bitmap(entArr[i][j]);
}

/* Constructor
*
*/
eShot::eShot(int speedx, int speedy, int life, int damage, int spriteType) {
  int seqArr[3] = {0, 1, 2};
  int seqTimeArr[3] = {1, 1, 1};
  __addBitmap( entArr[spriteType][0], 0, 0, 1);
  __addBitmap( entArr[spriteType][1], 0, 1, 1);
  __addBitmap( entArr[spriteType][2], 0, 2, 1);
  __setSequence(0, 3, seqArr, seqTimeArr);
  __changeSequence(0);

  _shotSpeedX = speedx;
  _shotSpeedY = speedy;
  _life = life;
  _type = TYPE_BULLET;
  _shotDamage = damage;
  _owner = TYPE_PLAYER;

  int bbx[2];
  if ( speedx > 0 ) {
    bbx[0] = speedx - 4;
    bbx[1] = 4;
  }
  if ( speedx < 0 ) {
    bbx[0] = 0;
    bbx[1] = (speedx < 4 ) ? 4 : speedx;
  }
  __setBoundingBox(bbx[0],0,bbx[1],4);
  __enableBoundingBox(1);
  __lockBoundingBox(1);
}

/* __update() :
*
*/
int eShot::__update() {
  __updateAnimation();
  __move(_shotSpeedX, _shotSpeedY);
  if ( --_life < 1 ) return 0;
  return 1;
}

/* __getDamage() :
*
*/
int eShot::__getDamage() { return _shotDamage; }

/* __setOwner()
*  __getOwner()
*/
void eShot::__setOwner(int nOwner) { _owner = nOwner; }
int eShot::__getOwner() { return _owner; }

eHit::eHit(BoundingBox nbb, int damage, int lifespan) {
  __setBoundingBox(nbb);
  __enableBoundingBox(1);
  __lockBoundingBox(1);  
  _life = lifespan;
  _hitDamage = damage;
  _type = TYPE_HIT;
  __setBitmap(entArr[2][0]);
}

int eHit::__update() {
  if ( --_life < 1 ) return 0;
  return 1; 
}
int eHit::__getDamage() {
  return _hitDamage;
}
